package gld.algo.park;

import java.util.*;

import gld.algo.park.BasicTLC.DecisionCycle;
import gld.infra.*;

/**
 * This class is written solely to facilitate calculation in BasicTLC and its
 * subclasses. An intersection is a junction with traffic lights.
 */
public class Intersection {
	Junction junction;

	AbstractTLC tlc;
	StageConfig stageConfig;

	LinkedList<Intersection> reachableIntersections;
//	LinkedList<Drivelane> inboundLanes = null;
	

	public Intersection(Junction junction, AbstractTLC tlc) {
		this.junction = junction;
		this.tlc = tlc;
		this.stageConfig = new StageConfig(this);
	}

	public Junction getJunction() {
		return junction;
	}
	
	public StageConfig getStageConfig() {
		return stageConfig;
	}

	public String toString() {
		return "Intersection " + junction.getId();
	}

	/**
	 * 
	 * @return Reachable neighboring intersections (alpha nodes)
	 * @throws InfraException
	 */
	public LinkedList<Intersection> getReachableIntersections()
			throws InfraException {
		// Use cache if it's calculated already
		if (reachableIntersections != null)
			return reachableIntersections;

		reachableIntersections = new LinkedList<Intersection>();
		Node n;
		for (Road r : junction.getAllRoads()) {

			// It's possible to have a null road
			if (r == null)
				continue;

			Drivelane[] ls = r.getOutboundLanes(junction);
			if (ls.length == 0)
				continue;
			n = ls[0].getNodeLeadsTo();
			// It has some bugs. TODO make a mapping of junction to
			// intersection, so
			// don't create a new intersection every time and duplicated.
			if (n.getType() == Node.JUNCTION
					&& ((Junction) n).getNumRealSigns() > 0)
				reachableIntersections.add(tlc.getIntersection(((Junction) n)));
		}
		return reachableIntersections;
	}
/*
	public LinkedList<Drivelane> getInboundLanes() throws InfraException {
		if (inboundLanes != null)
			return inboundLanes;

		inboundLanes = new LinkedList<Drivelane>();
		for (Drivelane l : junction.getInboundLanes()) {
			inboundLanes.add(l);
		}
		return inboundLanes;
	}
*/

	
}
